import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 登录
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login/index.vue"),
      meta: { requireAuth: false }
    },

    // 主页
    {
      path: "/",
      component: () => import("@/views/layout/index.vue"),
      redirect: '/home',
      children: [
        {
          path: "/home",
          name: 'home',
          component: () => import("@/views/home/index.vue"),
          meta: {
            requireAuth: true
          }
        },

        // 个人信息
        {
          path: '/user/userInfo',
          component: () => import('@/views/user/userInfo.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/user/password',
          component: () => import('@/views/user/password.vue'),
          meta: {
            requireAuth: true
          }
        },

        // 家长管理
        {
          path: '/family/familyInfo',
          component: () => import("@/views/family/familyInfo.vue"),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/family/childInfo',
          component: () => import("@/views/family/childInfo.vue"),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/family/childRest',
          component: () => import("@/views/family/childRest.vue"),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/family/familyApply',
          component: () => import("@/views/family/familyApply.vue"),
          meta: {
            requireAuth: true
          }
        },

        // 学生管理
        {
          path: '/student/payment',
          component: () => import("@/views/student/payment.vue"),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/student/diningRecord',
          component: () => import("@/views/student/diningRecord.vue"),
          meta: {
            requireAuth: true
          }
        },

        // 公告
        {
          path: '/platfrom/dish',
          component: () => import("@/views/platfrom/dish.vue"),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/platfrom/announcement',
          component: () => import("@/views/platfrom/announcement.vue"),
          meta: {
            requireAuth: true
          }
        },

        // 账号
        {
          path: '/accountApply',
          component: () => import("@/views/systemAccount/accountApply.vue"),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/accountRealas',
          component: () => import("@/views/systemAccount/accountRealas.vue"),
          meta: {
            requireAuth: true
          }
        }
      ],
      meta: {
        requireAuth: true
      }
    }
  ],
})

router.beforeEach((to, from, next) => {
  const requiresAuth = to.matched.some(record => record.meta.requireAuth)
  const hasToken = !!sessionStorage.token

  if (requiresAuth && !hasToken) {
    next({ name: 'login' })
  } else if (to.name === 'login' && hasToken) {
    next('/home') // 避免 name 解析失败
  } else {
    next()
  }
})

export default router
